/* ==============================================================================
*
*   Copyright (c) 2010, Pierre Favier
*   All rights reserved.
*
*   NTasking is free software: you can redistribute it and/or modify it
*   under the terms of the GNU General Public License as published by the
*   Free Software Foundation.
*
*   Redistribution and use in source and binary forms, with or without
*   modification, are permitted provided that the following conditions
*   are met:
*
*   -   Redistributions of source code must retain the above copyright notice,
*       this list of conditions and the following disclaimer.
*
*   -   Redistributions in binary form must reproduce the above
*       copyright notice, this list of conditions and the following
*       disclaimer in the documentation and/or other materials
*       provided with the distribution.
*
*   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
*   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
*   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
*   BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
*   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
*   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
*   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
*   ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
*   TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
*   THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
*   SUCH DAMAGE.
*
* ============================================================================== */

using System;
using NTasking.Internals;
using Void = NTasking.Internals.Void;

namespace NTasking
{

    /// <summary>
    /// Represent a synchronized method family with no parameters and no return type
    /// </summary>
    /// <typeparam name="IndexT">The type of this family's Index.</typeparam>
    public sealed class ActionFamily<IndexT> : MethodFamily<
        IndexT,
        Void,
        Action,
        Func<MethodCall<Void>>,
        Action<IndexT>>
    {

        internal ActionFamily() {}


        protected override Action CreateMemberDelegate(IndexT index)
        {
            return new ActionFamily<IndexT>
                            {
                                Owner = Owner,
                                Name = Name,
                                Family = Family,
                                Index = index
                            }.SynchronousCall();
        }


        /// <summary>
        /// Get a delegate which can be used to perform an direct call of this instance.
        /// </summary>
        /// <returns>
        /// A delegate which can be used to perform a direct call on this object
        /// </returns>
        protected override Action SynchronousCall()
        {
            return () => Owner.SynchronousCall<Void, Action<IndexT>>(Intercept());
        }


        /// <summary>
        /// Retrieve a delegate which can be used to capture the call of the current
        /// instance into a <see cref="MethodCall"/>
        /// </summary>
        /// <returns>
        /// A delegate which can be used to capture a call of the current instance
        /// </returns>
        internal override Func<MethodCall<Void>> Intercept
        {
            get { return () => new MethodCall<IndexT, Void>(Family, new CallContext<Void>(), Index); }
        }


        /// <summary>
        /// Execute the specified call using the specified method body and return the produced result.
        /// </summary>
        /// <param name="context">The parameters of the call.</param>
        /// <param name="body">The method body to be executed</param>
        /// <returns>The result of executing the call</returns>
        internal override Void Execute(CallContext context, Action<IndexT> body)
        {
            body(Index);
            return null;
        }

    }


    /// <summary>
    /// Represent a synchronized method family with 1 parameters and no return type
    /// </summary>
    /// <typeparam name="IndexT">The type of this family's Index.</typeparam>
    /// <typeparam name="P1T">The type of the first parameter</typeparam>
    public sealed class ActionFamily<IndexT, P1T> : MethodFamily<
        IndexT,
        Void,
        Action<P1T>,
        Func<P1T, MethodCall<Void>>,
        Action<IndexT, P1T>>
    {

        internal ActionFamily() { }


        protected override Action<P1T> CreateMemberDelegate(IndexT index)
        {
            return new ActionFamily<IndexT, P1T>
                            {
                                Owner = Owner,
                                Name = Name,
                                Family = Family,
                                Index = index
                            }.SynchronousCall();
        }


        /// <summary>
        /// Get a delegate which can be used to perform an direct call of this instance.
        /// </summary>
        /// <returns>
        /// A delegate which can be used to perform a direct call on this object
        /// </returns>
        protected override Action<P1T> SynchronousCall()
        {
            return (p1) => Owner.SynchronousCall<Void, Action<IndexT, P1T>>(Intercept(p1));
        }


        /// <summary>
        /// Retrieve a delegate which can be used to capture the call of the current
        /// instance into a <see cref="MethodCall"/>
        /// </summary>
        /// <returns>
        /// A delegate which can be used to capture a call of the current instance
        /// </returns>
        internal override Func<P1T, MethodCall<Void>> Intercept
        {
            get { return (p1) => new MethodCall<IndexT, Void>(Family, new CallContext<P1T, Void>(p1), Index); }
        }


        /// <summary>
        /// Execute the specified call using the specified method body and return the produced result.
        /// </summary>
        /// <param name="context">The parameters of the call.</param>
        /// <param name="body">The method body to be executed</param>
        /// <returns>The result of executing the call</returns>
        internal override Void Execute(CallContext context, Action<IndexT, P1T> body)
        {
            IParameters<P1T> p = (IParameters<P1T>)context;

            body(Index, p.P1);
            return null;
        }

    }


    /// <summary>
    /// Represent a synchronized method family with 2 parameters and no return type
    /// </summary>
    /// <typeparam name="IndexT">The type of this family's Index.</typeparam>
    /// <typeparam name="P1T">The type of the first parameter</typeparam>
    /// <typeparam name="P2T">The type of the second parameter</typeparam>
    public sealed class ActionFamily<IndexT, P1T, P2T> : MethodFamily<
        IndexT,
        Void,
        Action<P1T, P2T>,
        Func<P1T, P2T, MethodCall<Void>>,
        Action<IndexT, P1T, P2T>>
    {

        internal ActionFamily() { }


        protected override Action<P1T, P2T> CreateMemberDelegate(IndexT index)
        {
            return new ActionFamily<IndexT, P1T, P2T>
                            {
                                Owner = Owner,
                                Name = Name,
                                Family = Family,
                                Index = index
                            }.SynchronousCall();
        }


        /// <summary>
        /// Get a delegate which can be used to perform an direct call of this instance.
        /// </summary>
        /// <returns>
        /// A delegate which can be used to perform a direct call on this object
        /// </returns>
        protected override Action<P1T, P2T> SynchronousCall()
        {
            return (p1, p2) => Owner.SynchronousCall<Void, Action<IndexT, P1T, P2T>>(Intercept(p1, p2));
        }


        /// <summary>
        /// Retrieve a delegate which can be used to capture the call of the current
        /// instance into a <see cref="MethodCall"/>
        /// </summary>
        /// <returns>
        /// A delegate which can be used to capture a call of the current instance
        /// </returns>
        internal override Func<P1T, P2T, MethodCall<Void>> Intercept
        {
            get { return (p1, p2) => new MethodCall<IndexT, Void>(Family, new CallContext<P1T, P2T, Void>(p1, p2), Index); }
        }


        /// <summary>
        /// Execute the specified call using the specified method body and return the produced result.
        /// </summary>
        /// <param name="context">The parameters of the call.</param>
        /// <param name="body">The method body to be executed</param>
        /// <returns>The result of executing the call</returns>
        internal override Void Execute(CallContext context, Action<IndexT, P1T, P2T> body)
        {
            IParameters<P1T, P2T> p = (IParameters<P1T, P2T>)context;

            body(Index, p.P1, p.P2);
            return null;
        }

    }


    /// <summary>
    /// Represent a synchronized method family with 3 parameters and no return type
    /// </summary>
    /// <typeparam name="IndexT">The type of this family's Index.</typeparam>
    /// <typeparam name="P1T">The type of the first parameter</typeparam>
    /// <typeparam name="P2T">The type of the second parameter</typeparam>
    /// <typeparam name="P3T">The type of the third parameter</typeparam>
    public sealed class ActionFamily<IndexT, P1T, P2T, P3T> : MethodFamily<
        IndexT,
        Void,
        Action<P1T, P2T, P3T>,
        Func<P1T, P2T, P3T, MethodCall<Void>>,
        Action<IndexT, P1T, P2T, P3T>>
    {

        internal ActionFamily() { }


        protected override Action<P1T, P2T, P3T> CreateMemberDelegate(IndexT index)
        {
            return new ActionFamily<IndexT, P1T, P2T, P3T>
            {
                Owner = Owner,
                Name = Name,
                Family = Family,
                Index = index
            }.SynchronousCall();
        }


        /// <summary>
        /// Get a delegate which can be used to perform an direct call of this instance.
        /// </summary>
        /// <returns>
        /// A delegate which can be used to perform a direct call on this object
        /// </returns>
        protected override Action<P1T, P2T, P3T> SynchronousCall()
        {
            return (p1, p2, p3) => Owner.SynchronousCall<Void, Action<IndexT, P1T, P2T, P3T>>(Intercept(p1, p2, p3));
        }


        /// <summary>
        /// Retrieve a delegate which can be used to capture the call of the current
        /// instance into a <see cref="MethodCall"/>
        /// </summary>
        /// <returns>
        /// A delegate which can be used to capture a call of the current instance
        /// </returns>
        internal override Func<P1T, P2T, P3T, MethodCall<Void>> Intercept
        {
            get { return (p1, p2, p3) => new MethodCall<IndexT, Void>(Family, new CallContext<P1T, P2T, P3T, Void>(p1, p2, p3), Index); }
        }


        /// <summary>
        /// Execute the specified call using the specified method body and return the produced result.
        /// </summary>
        /// <param name="context">The parameters of the call.</param>
        /// <param name="body">The method body to be executed</param>
        /// <returns>The result of executing the call</returns>
        internal override Void Execute(CallContext context, Action<IndexT, P1T, P2T, P3T> body)
        {
            IParameters<P1T, P2T, P3T> p = (IParameters<P1T, P2T, P3T>)context;

            body(Index, p.P1, p.P2, p.P3);
            return null;
        }

    }


    /// <summary>
    /// Represent a synchronized method family with 4 parameters and no return type
    /// </summary>
    /// <typeparam name="IndexT">The type of this family's Index.</typeparam>
    /// <typeparam name="P1T">The type of the first parameter</typeparam>
    /// <typeparam name="P2T">The type of the second parameter</typeparam>
    /// <typeparam name="P3T">The type of the third parameter</typeparam>
    /// <typeparam name="P4T">The type of the fourth</typeparam>
    public sealed class ActionFamily<IndexT, P1T, P2T, P3T, P4T> : MethodFamily<
        IndexT,
        Void,
        Action<P1T, P2T, P3T, P4T>,
        Func<P1T, P2T, P3T, P4T, MethodCall<Void>>,
        Action<IndexT, P1T, P2T, P3T, P4T>>
    {

        internal ActionFamily() { }


        protected override Action<P1T, P2T, P3T, P4T> CreateMemberDelegate(IndexT index)
        {
            return new ActionFamily<IndexT, P1T, P2T, P3T, P4T>
            {
                Owner = Owner,
                Name = Name,
                Family = Family,
                Index = index
            }.SynchronousCall();
        }


        /// <summary>
        /// Get a delegate which can be used to perform an direct call of this instance.
        /// </summary>
        /// <returns>
        /// A delegate which can be used to perform a direct call on this object
        /// </returns>
        protected override Action<P1T, P2T, P3T, P4T> SynchronousCall()
        {
            return (p1, p2, p3, p4) => Owner.SynchronousCall<Void, Action<IndexT, P1T, P2T, P3T, P4T>>(Intercept(p1, p2, p3, p4));
        }


        /// <summary>
        /// Retrieve a delegate which can be used to capture the call of the current
        /// instance into a <see cref="MethodCall"/>
        /// </summary>
        /// <returns>
        /// A delegate which can be used to capture a call of the current instance
        /// </returns>
        internal override Func<P1T, P2T, P3T, P4T, MethodCall<Void>> Intercept
        {
            get { return (p1, p2, p3, p4) => new MethodCall<IndexT, Void>(Family, new CallContext<P1T, P2T, P3T, P4T, Void>(p1, p2, p3, p4), Index); }
        }


        /// <summary>
        /// Execute the specified call using the specified method body and return the produced result.
        /// </summary>
        /// <param name="context">The parameters of the call.</param>
        /// <param name="body">The method body to be executed</param>
        /// <returns>The result of executing the call</returns>
        internal override Void Execute(CallContext context, Action<IndexT, P1T, P2T, P3T, P4T> body)
        {
            IParameters<P1T, P2T, P3T, P4T> p = (IParameters<P1T, P2T, P3T, P4T>)context;

            body(Index, p.P1, p.P2, p.P3, p.P4);
            return null;
        }

    }

}
